import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/values/colors.dart';
import 'package:enterprise_v6_app/app/core/values/dimens.dart';
import 'package:enterprise_v6_app/app/core/values/gaps.dart';
import 'package:enterprise_v6_app/app/core/values/images.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/accident_info_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/basic_information/accident_events/accident_events_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

///事故事件
class AccidentEventsPage extends StatelessWidget {
  AccidentEventsPage({super.key});

  final controller = Get.put(AccidentEventsController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
            color: AppColors.bg_color_grey,
            padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
            child: Column(
              children: [
                Row(
                  children: [
                    Expanded(
                        child: CustomSearchEnter(
                      height: 32.w,
                      hintText: '请输入事故事件',
                      text: controller.search,
                      onCallBack: (value) {
                        controller.search = value;
                        controller.refreshPage();
                      },
                    )),
                    AppGaps.hGap8,
                    _screen(),
                  ],
                ),
                AppGaps.vGap12,
                Expanded(
                    child: CustomEasyRefreshList<AccidentInfoEntity>(
                  key: controller.globalKey,
                  dataLoad: (pageNum, pageSize, {search}) {
                    return controller.getListData(pageNum, pageSize);
                  },
                  itemBuilder: (item, index) {
                    return CustomTitleChartPage(
                      title: item.accidentName,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: _rowData('处置状态：', item.disposalStatusName),
                              ),
                              Expanded(
                                flex: 1,
                                child: _rowData('上报状态：', item.reportingStatusName),
                              )
                            ],
                          ),
                          AppGaps.vGap12,
                          Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: _rowData('事故来源：', item.accidentSourceName),
                              ),
                              Expanded(
                                flex: 1,
                                child: _rowData('事故等级：', item.accidentLevelName, valueColor: const Color(0xFFFD6B6B)),
                              )
                            ],
                          ),
                          AppGaps.vGap12,
                          _rowData('事故类型：', item.accidentTypeName),
                          AppGaps.vGap12,
                          Container(
                            padding: EdgeInsets.symmetric(vertical: 10.w),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(8),
                              color: const Color(0x1A5C6DFF),
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                Column(
                                  children: [
                                    _topTitle('${item.deathNum}', const Color(0xFFFD6B6B), '死亡人数', const Color(0xFF5C6DFF)),
                                  ],
                                ),
                                _divider(),
                                Column(
                                  children: [
                                    _topTitle('${item.seriousInjuryNum}', const Color(0xFFFC9900), '重伤人数', const Color(0xFF5C6DFF)),
                                  ],
                                ),
                                _divider(),
                                Column(
                                  children: [
                                    _topTitle('${item.minorInjuryNum}', const Color(0xFFFFCE24), '轻伤人数', const Color(0xFF5C6DFF)),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ).click(() {
                      Get.toNamed(AppRoutes.accidentEventsDetail, arguments: item.id);
                    });
                  },
                )),
              ],
            ),
          ), // 右侧筛选栏
          key: controller.popRightScaffoldKey,
          // 右侧筛选栏
          endDrawer: Container(
            width: AppDimens.sw - 80.w,
            height: AppDimens.sh,
            color: Colors.white,
            child: _getFilterWidget(),
          ),
        );
      },
    );
  }

  ///获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.filterList,
      onConfirm: controller.screenConfirm,
      topSpacing: AppGaps.vGap20,
      isRadio: true,
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 16.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.vGap4,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 14.sp,
            color: square,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  //自定义排列
  Widget _rowData(String name, String value, {Color valueColor = const Color(0xFF495061)}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            height: 1.2,
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            color: valueColor,
            height: 1.2,
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )),
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 16.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }
}
